import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:toolbox/common/common.dart';

import '../../../manager/hive/hiveHelper.dart';
import '../../../manager/tools.dart';
import '../../../models/WebBookmarkModel.dart';
import '../../../widgets/defaultDialog.dart';

class EditBookmarkDialog extends StatefulWidget {
  final WebBookmarkModel model;
  EditBookmarkDialog({super.key, required this.model});

  @override
  State<EditBookmarkDialog> createState() => _EditBookmarkDialogState();
}

class _EditBookmarkDialogState extends State<EditBookmarkDialog> {
  TextEditingController nameController = TextEditingController();
  TextEditingController urlController = TextEditingController();

  @override
  void initState() {
    super.initState();
    nameController.text = widget.model.name;
    urlController.text = widget.model.url;
  }

  back() {
    Get.back();
  }

  void _onTapDelete() {
    Get.dialog(
      defaultDialog(
          title: '删除书签',
          content: widget.model.url,
          sureFunction: () {
            HiveHelper.instance.deleteWebStar(widget.model.url);
            setState(() {});
            back();
          }
      ),
      barrierDismissible: false,
      useSafeArea: false,
    );
  }

  void _onTapSave() {
    widget.model.name = nameController.text;
    widget.model.url = urlController.text;
    HiveHelper.instance.updateWebStar(widget.model);
    back();
  }
  
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Stack(
              children: [
                Container(
                  margin: const EdgeInsets.symmetric(horizontal: 20),
                  padding: const EdgeInsets.all(20),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(16),
                    color: Colors.white,
                  ),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children:[
                      TextFormField(
                        controller: nameController,
                        decoration: const InputDecoration(
                          labelText: '名称',
                        ),
                      ),
                      TextFormField(
                        controller: urlController,
                        decoration: const InputDecoration(
                          labelText: 'url',
                        ),
                      ),
                      const SizedBox(height: 16),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children:[
                          button('删除', _onTapDelete, false),
                          button(Strings.save, _onTapSave, true),
                        ]
                      ),
                    ],
                  ),
                ),
                Positioned(
                  right: 25,
                  top: 0,
                  child: IconButton(
                    onPressed: back,
                    icon: const Icon(Icons.close),
                  ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget button(String text, Function() onTap, bool active) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 10),
        decoration: BoxDecoration(
          color: active ? Colors.blue : null,
          border: Border.all(color: Colors.blue),
          borderRadius: BorderRadius.circular(12)
        ),
        child: Text(
          text,
          style: Styles.textStyle(size: 16, color: active ? Colors.white : Colors.blue),
        ),
      ),
    );
  }
}
